using UnityEngine;
using System.Collections;

public class ZombieControl : StateBehaviour<ZombieControl> 
{
    public float LIFE = 10f;
    public float DAMAGE = 2f;
    public float ATACKSPEED = 1f;     //mientras menor sea, mas rapido
    public float RUNSPEED = 1f;
	public GameObject dropBala;
	public GameObject dropVida;

    public ColeccionPuntos pathFinding;
	
	[HideInInspector]
	public float time;
	[HideInInspector]
	public Vector3[] destinos;
    [HideInInspector]
    public Vector3 destino;
    [HideInInspector]
    public Vector3 posicion;
    [HideInInspector]
    public int contador;
	[HideInInspector]
    public Random random;
    [HideInInspector]
    public NavMeshAgent controller;

    [HideInInspector]
    public Transform enemigoActual;
	
	private TorretaControl torreControl;
	private PlayerControl playerControl;
	private HospitalControl  hospitalControl;
	private Transform player;
	private bool playerCerca = false;
	private bool muerto = false;
	private bool hospitalCerca = false;

	void Start () 
    {
		
        this.destinos = this.pathFinding.m_puntos;
        this.controller = this.GetComponent<NavMeshAgent>();
        this.controller.speed = RUNSPEED;
        this.updateCalledIn = StateBehaviourMethodsName.FixedUpdate;
		
		contador = Random.Range(1, destinos.Length);
//		Debug.Log("_________random "+ contador);		
        this.State = StateZombieRun.instance;
	}

    public override void OnUpdate()
    {
    }

    public override ZombieControl GetBehaviuor()
    {
        return this;
    }

    void OnTriggerEnter(Collider other)
    {
		// trigger player visto
	    if (other.name == "TriggerVisto")
		{
			player = other.GetComponent<Transform>().parent;
			playerCerca = true;
		}
		// trigger player melee
		else if (other.name == "TriggerMelee")
		{
			enemigoActual = other.GetComponent<Transform>().parent;			
			if(!muerto){
            	this.State = StateZombieAtack.instance;
			}
		} 
		// zombies atacan hospital?
			/*
		// trigger hospital
		else if (other.name == "TriggerHospital")
		{
			enemigoActual = other.GetComponent<Transform>().parent;
			hospitalCerca = true;
			if(!muerto){
            	this.State = StateZombieAtack.instance;
			}
		}
	        */
    }
	
	void OnTriggerExit(Collider other)
    {
		if (other.name == "TriggerVisto")
		{
			playerCerca = false;
		}
		else if (other.name == "TriggerMelee")
		{
			if(!muerto){
				this.State = StateZombieRun.instance;
			}
		}
	}

  	public void ComprobarDestino()
	{
		Vector3 coorActual = destino - transform.position;
		
		if (playerCerca)
		{			
			destino = player.position;
		}
		else
		{
			if (coorActual.magnitude < 1.0f)
			{
            	contador = Random.Range(1, destinos.Length);
				destino = this.destinos[contador];				
			}				
		}		
	}
	
	public void RecibirDmg(float dmg)
	{
        LIFE = LIFE - dmg;
        Debug.Log("------Zombie_life_left: " + LIFE);
        if (LIFE <= 0)
        {
            this.State = StateZombieDie.instance;
        }
	}

    public void ProducirDmg()
    {
        Debug.Log("ZOMBIE ATACK!!");
        if (enemigoActual != null)
		{
            //enemigoActual.SendMessage("RecibirDmg", DAMAGE);
			if (playerCerca)
			{
				playerControl = enemigoActual.GetComponent("PlayerControl") as PlayerControl;
				playerControl.RecibirDmg(DAMAGE);
				
			}
			// zombies atacan hospital?
			/*
 			if(hospitalCerca)
			{
				hospitalControl = enemigoActual.GetComponent("HospitalControl") as HospitalControl;
				hospitalControl.RecibirDmg(DAMAGE);
			}
			*/
		}
    }

    public void Destruir()
    {
        Debug.Log("::::::::: destruimos zombie ::::::::::");
		muerto = true;
        animation.Play ("Morir");
        Invoke("Dropear", 4);
		Destroy(this.gameObject,4);
		
    }
	
	public void Dropear()
	{
		GameObject clonVida = (GameObject) Instantiate(dropVida, transform.position, Quaternion.identity);
		clonVida.GetComponent<PowerUpvida>().cantidad = 10;					
	}
	
	public void EnemigoMuerto(Transform tr)
    {
       

    }
}
